"""LeadIntention model for storing additional customer intentions."""

from __future__ import annotations

from typing import TYPE_CHECKING

from sqlalchemy import Boolean, ForeignKey, Integer, String, Text, Enum as SQLEnum
from sqlalchemy.orm import Mapped, mapped_column, relationship

from ..db.base_class import Base
from ..models.base import TimestampMixin
from ..models.enums import DemandType, IntentionLevel

if TYPE_CHECKING:
    from ..models.lead import Lead


class LeadIntention(Base, TimestampMixin):
    """
    客户多意向表（可选补充）.

    业务逻辑：
    1. Lead表存储主意向（必填）
    2. LeadIntention表存储额外意向（可选，可为空）
    3. 一个客户可以有0-N个额外意向

    使用场景：
    - 客户有多个租房需求（整租 + 合租备选）
    - 客户短期租房 + 长期买房规划
    - 不同区域、不同预算的多个意向
    """

    __tablename__ = "lead_intentions"

    id: Mapped[int] = mapped_column(primary_key=True, index=True)

    # 外键：关联客户
    lead_id: Mapped[int] = mapped_column(
        ForeignKey("leads.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
        comment="客户ID",
    )

    # 意向内容
    demand_type: Mapped[DemandType] = mapped_column(
        SQLEnum(DemandType), nullable=False, index=True, comment="需求类型"
    )

    budget_min: Mapped[int | None] = mapped_column(
        Integer, nullable=True, index=True, comment="预算下限（元/月 或 总价元）"
    )

    budget_max: Mapped[int | None] = mapped_column(
        Integer, nullable=True, index=True, comment="预算上限（元/月 或 总价元）"
    )

    area_preferences: Mapped[str | None] = mapped_column(
        String(500), nullable=True, comment='区域偏好（JSON数组字符串，如: ["望京","国贸"]）'
    )

    layout: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="户型需求（如: 2室1厅）")

    requirement_tags: Mapped[str | None] = mapped_column(
        String(500), nullable=True, comment='需求标签（JSON数组字符串，如: ["近地铁","可养宠物"]）'
    )

    # 优先级和意向度
    priority: Mapped[int] = mapped_column(
        Integer, nullable=False, default=1, comment="优先级（1-10，数字越小优先级越高）"
    )

    intention_level: Mapped[IntentionLevel] = mapped_column(
        SQLEnum(IntentionLevel),
        nullable=False,
        default=IntentionLevel.MEDIUM,
        index=True,
        comment="意向度",
    )

    # 备注
    notes: Mapped[str | None] = mapped_column(Text, nullable=True, comment="该意向的详细备注")

    # 关系
    lead: Mapped[Lead] = relationship("Lead", back_populates="intentions")
